/*
 * ConsultarAlimentos.java
 *
 * Created on 09-may-2012, 15:55:13
 */

package interfazgrafica.consultar.alimentos;

import interfazgrafica.ayuda.VentanaAyuda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Lauaa
 */
public class ConsultarAlimentos extends javax.swing.JFrame {
    private final Connection conn;
    private final Statement stmt;

    /** Creates new form ConsultarAlimentos */
    public ConsultarAlimentos() throws ClassNotFoundException, SQLException {
        initComponents();
        //Accedo a la base de datos.
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "terrano89");
            stmt = conn.createStatement();

        //Hago una consulta y la guardo en rs.
	ResultSet rs = null;
            rs = stmt.executeQuery("SELECT numerodocumento, fechacompra, naturaleza, cantidad FROM Alimentos");

                DefaultTableModel modelo = new DefaultTableModel();
                jTable1.setModel(modelo);

                // Creamos las columnas.
                modelo.addColumn("Numero de documento");
                modelo.addColumn("Fecha de Compra");
                modelo.addColumn("Naturaleza");
                modelo.addColumn("Cantidad");

                // Bucle para cada resultado en la consulta
                while (rs.next())
                {
                   // Se crea un array que será una de las filas de la tabla.
                   Object [] fila = new Object[4]; // Hay tres columnas en la tabla

                   // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
                   for (int i=0;i<4;i++)
                      fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                   // Se añade al modelo la fila completa.
                   modelo.addRow(fila);
                }
                jTable1.setModel(modelo);

            rs.close();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextFieldNumeroDocumento = new javax.swing.JTextField();
        jLabelFecha = new javax.swing.JLabel();
        jLabelCantidad = new javax.swing.JLabel();
        jLabelNumeroDocumento = new javax.swing.JLabel();
        jTextFieldNaturaleza = new javax.swing.JTextField();
        jLabeNaturaleza = new javax.swing.JLabel();
        jTextFieldCantidad = new javax.swing.JTextField();
        jTextFieldFecha = new javax.swing.JTextField();
        jButtonAyuda = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextFieldNumeroDocumento.setName("jTextFieldNumeroDocumento"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cowmanager.CowManagerApp.class).getContext().getResourceMap(ConsultarAlimentos.class);
        jLabelFecha.setText(resourceMap.getString("jLabelFecha.text")); // NOI18N
        jLabelFecha.setName("jLabelFecha"); // NOI18N

        jLabelCantidad.setText(resourceMap.getString("jLabelCantidad.text")); // NOI18N
        jLabelCantidad.setName("jLabelCantidad"); // NOI18N

        jLabelNumeroDocumento.setText(resourceMap.getString("jLabelNumeroDocumento.text")); // NOI18N
        jLabelNumeroDocumento.setName("jLabelNumeroDocumento"); // NOI18N

        jTextFieldNaturaleza.setName("jTextFieldNaturaleza"); // NOI18N

        jLabeNaturaleza.setText(resourceMap.getString("jLabeNaturaleza.text")); // NOI18N
        jLabeNaturaleza.setName("jLabeNaturaleza"); // NOI18N

        jTextFieldCantidad.setName("jTextFieldCantidad"); // NOI18N

        jTextFieldFecha.setName("jTextFieldFecha"); // NOI18N

        jButtonAyuda.setText(resourceMap.getString("jButtonAyuda.text")); // NOI18N
        jButtonAyuda.setName("jButtonAyuda"); // NOI18N
        jButtonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAyudaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelFecha)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelNumeroDocumento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addComponent(jTextFieldNumeroDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabeNaturaleza)
                    .addComponent(jLabelCantidad))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldNaturaleza, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                    .addComponent(jTextFieldCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                .addGap(35, 35, 35))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(499, Short.MAX_VALUE)
                .addComponent(jButtonAyuda, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabeNaturaleza)
                    .addComponent(jTextFieldNaturaleza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFecha)
                    .addComponent(jTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad)
                    .addComponent(jTextFieldCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNumeroDocumento)
                    .addComponent(jTextFieldNumeroDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(jButtonAyuda)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            //Hago una consulta y la guardo en rs2.
            ResultSet rs2 = null;
            String consulta = "SELECT * FROM Alimentos WHERE numerodocumento = '";
            consulta = consulta + jTable1.getValueAt(jTable1.getSelectedRow(), 0) + "'";
            rs2 = stmt.executeQuery(consulta);
            while (rs2.next()) {
                jTextFieldCantidad.setText(rs2.getString("cantidad"));
                jTextFieldNumeroDocumento.setText(rs2.getString("numerodocumento"));
                jTextFieldNaturaleza.setText(rs2.getString("naturaleza"));
                jTextFieldFecha.setText(rs2.getString("fechacompra"));
            }
            rs2.close();
        } catch (SQLException ex) {
            Logger.getLogger(ConsultarAlimentos.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jTable1MouseClicked

    private void jButtonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAyudaActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Consultar alimentos");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jButtonAyudaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ConsultarAlimentos().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ConsultarAlimentos.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(ConsultarAlimentos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAyuda;
    private javax.swing.JLabel jLabeNaturaleza;
    private javax.swing.JLabel jLabelCantidad;
    private javax.swing.JLabel jLabelFecha;
    private javax.swing.JLabel jLabelNumeroDocumento;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldCantidad;
    private javax.swing.JTextField jTextFieldFecha;
    private javax.swing.JTextField jTextFieldNaturaleza;
    private javax.swing.JTextField jTextFieldNumeroDocumento;
    // End of variables declaration//GEN-END:variables

}
